<?php

namespace app\admin\controller;

use app\common\Helper;
use app\admin\model\Type as TypeMdl;

use think\Db;
use think\Exception;
use think\Request;

/**
 * 分类
 */
class Type extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new TypeMdl();
    }

    /**
     * 列表
     */
    public function getList(){
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [];
        $model = $this->model;
        $order = 'sorting ASC,updateTime DESC';
        $result = $model->where($where)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();

        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关分类');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 添加/编辑 保存
     */
    public function save(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->typeName)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeName 不能为空');
        }
        if (empty($param->imgUrl)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'imgUrl 不能为空');
        }

        $data = (array)$param;
        if (!empty($param->typeId)) {
            // 修改
            $id = $param->typeId;
            $data['updateTime'] = date('Y-m-d H:i:s');
            $data['updateOperator'] = $this->admin->id;
            $result = $model->allowField(true)->save($data, ['id' => $id]);

            // Query执行后的操作
            $model->_after_update($data);

        } else {
            // 新增
            $data['id'] = Helper::getUUID();
            $data['createTime'] = date('Y-m-d H:i:s');
            $data['createOperator'] = $this->admin->id;
            $maxSort = $model->order('sorting DESC')->value('sorting');
            $data['sorting'] = $maxSort + 1;
            $result = $model->save($data);

            // Query执行后的操作
            $model->_after_insert($data);
        }

        if ($result !== false) {
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '保存成功');
        } else {
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
        }
    }

    /**
     * 删除
     */
    public function delete(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->typeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeId 不能为空');
        }

        $id = $param->typeId;
        Db::startTrans();
        try {
            $result = $model->where('id', $id)->delete();
            if($result !== false){
                // Query执行后的操作
                $model->_after_delete($id);

                Db::commit();
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '删除成功');
            }else{
                Db::rollback();
                return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $e->getMessage());
        }
    }

    /**
     * 移动
     */
    public function move(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->typeId1)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeId1 不能为空');
        }
        if (empty($param->sorting1)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'sorting1 不能为空');
        }
        if (empty($param->typeId2)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeId2 不能为空');
        }
        if (empty($param->sorting2)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'sorting2 不能为空');
        }

        $data1['sorting'] = $param->sorting1;
        $result1 = $model->allowField(true)->save($data1, ['id' => $param->typeId1]);

        $data2['sorting'] = $param->sorting2;
        $result2 = $model->allowField(true)->save($data2, ['id' => $param->typeId2]);

        if($result1 !== false || $result2 !== false){
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '操作成功');
        }else{
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
        }
    }

}